/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package devbureau.servlet;

import devbureau.fstore.common.ItemData;
import devbureau.fstore.common.StoreFactory;
import devbureau.fstore.common.print.report.PrintReminderReport;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import static devbureau.fstore.util.Utilities.DateTime.formatShort;
/**
 *
 * @author admin
 */
public class RemindersServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(RemindersServlet.class);

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String excel = request.getParameter(Constants.EXCEL);
        OutputStream out = null;
        try {
            List<ItemData> reminders = StoreFactory.getStore().getBalanceReport();
            if(isTrue(excel)){
                response.setContentType(".xls application/vnd.ms-excel");
                response.setHeader( "Content-Disposition", "attachment; filename=\"reminder_report_"+formatShort(new Date())+".xls\"");
                out = response.getOutputStream();
                new PrintReminderReport(reminders,out).writeReport();
            }else{
                request.setAttribute(Constants.REMINDER, reminders);
                request.getRequestDispatcher("reports.jsp").forward(request, response);
            }
        }
        catch (Exception e) {
            log.error("Error while getting xls for bill report. " + e.getMessage());
            request.setAttribute(Constants.MESSAGE_ERROR, e.getMessage());
            
        }finally {
            if(out!=null){
                out.flush();
                out.close();
            }
        }
    }

    private boolean isTrue(String s){
        boolean isTrue = false;
        try{
            isTrue = Boolean.valueOf(s.trim().toLowerCase());
        }catch(Exception e){
            
        }
        return isTrue;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
